package com.base.study.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.study.R;
import com.base.study.adapter.MyStudyRecyclerAdapter;
import com.base.study.adapter.OnlineStudyRecyclerAdapter;
import com.base.study.bean.OnlineStudyListBean;
import com.base.study.bean.ScopeBean;
import com.base.study.databinding.FragmentOnlineStudyBinding;
import com.base.study.presenter.OnlineStudyListPresenter;
import com.base.study.view.OnlineStudyListView;
import com.base.study.widget.ConfirmPopWindow;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 在线学习
 * Created by zw on 2020/2/13.
 */

public class OnlineStudyFragment extends CBaseMvpFragment<OnlineStudyListPresenter> implements OnlineStudyListView {

    private FragmentOnlineStudyBinding mBinding;
    private List<OnlineStudyListBean> mData = new ArrayList<>();
    //private BaseAdapter<OnlineStudyListBean.Rows> mAdapter;
    private int page = 1;
    private OnlineStudyRecyclerAdapter onlineStudyRecyclerAdapter;


    @Override
    public void init() {
        mBinding = (FragmentOnlineStudyBinding) mRootBinding;
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mPresenter = new OnlineStudyListPresenter(this, getActivity());
    }

    @Override
    public void start() {
        initRecyClerView();
        mPresenter.getScope("edu");

    }

    private void initRecyClerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvOnlineList.setLayoutManager(linearLayoutManager);
        mBinding.rvOnlineList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 0, 10, 10));
        onlineStudyRecyclerAdapter = new OnlineStudyRecyclerAdapter(mContext, mData);
        mBinding.rvOnlineList.setAdapter(onlineStudyRecyclerAdapter);
        setHeaderView(mBinding.rvOnlineList);
        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getScope("edu");
                //mPresenter.getList(page,10);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getScope("edu");
                //mPresenter.getList(page,10);
            }
        });


    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_online_study;
    }

    @Override
    public void returnNoData() {
        mBinding.smartRefreshLayout.finishRefresh();
        if (onlineStudyRecyclerAdapter != null) {
            onlineStudyRecyclerAdapter.notifyDataSetChanged();
        }
        //mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(OnlineStudyListBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        //mData.addAll(bean);
        if (onlineStudyRecyclerAdapter != null) {
            onlineStudyRecyclerAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnMoreList(OnlineStudyListBean bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
//        mData.addAll(bean
//                );
        if (onlineStudyRecyclerAdapter != null) {
            onlineStudyRecyclerAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }

    private ScopeBean mScopeBean = new ScopeBean();

    @Override
    public void returnScope(ScopeBean scopeBean) {
        mScopeBean = scopeBean;
        if (header != null) {
            if (online_header_scopsse != null) {
                online_header_scopsse.setText(scopeBean.getRealAllScore() + "");
            }
        }

        mPresenter.getList(page, 10);
    }

    TextView online_header_scopsse;
    View header;

    private void setHeaderView(RecyclerView view) {
        header = LayoutInflater.from(mContext).inflate(R.layout.header_online_study, view, false);
        online_header_scopsse = (TextView) header.findViewById(R.id.online_header_scopsse);
        TextView online_header_scopsse_rule = (TextView) header.findViewById(R.id.online_header_scopsse_rule);
        LinearLayout online_header_scopsse_ranking = (LinearLayout) header.findViewById(R.id.online_header_scopsse_ranking);
        //online_header_scopsse.setText(String.valueOf(mScopeBean.getRealAllScore()));

        /**
         * 跳转积分规则
         */
        online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key", 1)
                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
                        .withString("title", "江苏中越科技")
                        .withString("name", "测试")
                        .navigation();
            }
        });


        /**
         * 跳转积分排行
         */
        online_header_scopsse_ranking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key", 1)
                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRank")
                        .withString("title", "江苏中越科技")
                        .withString("name", "测试")
                        .navigation();
            }
        });
        onlineStudyRecyclerAdapter.setHeaderView(header);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }

}
